<!-- 溯源记录-繁殖信息-->
<template>
	<info-title-list :infoTitleList="animalInfoList0"></info-title-list>
  <view>
    <StepsComponent :steps-data="stepsData" />
  </view>
</template>

<script setup>
import StepsComponent from '/components/Livestock/StepsComponent.vue'
import InfoTitleList from '@/components/Animals/InfoTitleList.vue';
import { getBreedingInfo } from '@/api/traceability'
import { ref, onMounted } from 'vue';

// 获取 Storage 中的 F_Id
const getFIdFromStorage = () => {
  const fId = uni.getStorageSync('F_Id');
  console.log('从 Storage 获取的 F_Id:', fId);
  return fId || null;
};

const animalInfoList0 = [
  {
    title: '繁殖记录'
  },

];
// 定义步骤条数据
const stepsData = ref([
  { 
	title: '出生',
	title1: '出生时间', content1: '2024-01-24',
    title2: '公畜', content2: 'IMEI：866452264124',
    title3: '母畜', content3: 'IMEI：866452264122',
    title4: '出生体重', content4: '10KG', 
    title5: '操作员', content5: 'xxxx',
   },

  { 
	title: '发情', 
	title1: '预测发情时间', content1: '2024-01-24',
    title2: '实际发情时间', content2: '2024-01-24',
    title3: '配种时间', content3: '2024-01-24',
   },

  { 
	  title: '配种',
	  title1: '配种时间', content1: '2024-01-24',
	  title2: '配种牲畜', content2: 'IMEI：866452264122', 
	  title3: '操作员', content3: 'xxxx',
	   },
 { 
	  title: '生育',
	  title1: '生育时间', content1: '2024-01-24',
	  title2: '生育牲畜', content2: 'IMEI：866452264122', 
	  title3: '操作员', content3: 'xxxx',
	   },
 { 
	  title: '节育',
	  title1: '配种时间', content1: '2024-01-24',
	  title2: '操作员', content2: 'xxxx',
	
	   },	   
]);

const fetchBreedingInfo = async () => {
  const fId = getFIdFromStorage();
  if (!fId) {
    console.error('F_Id 不存在，无法获取牲畜详细信息');
    return;
  }

  try {
    // 调用接口获取数据
    const response = await getBreedingInfo({ id: fId });
    console.log('获取到的牲畜详细信息:', response);
    
    if(response.code === 200 && response.data) {
      const data = response.data;
      
      // 处理出生信息
      const birthInfo = data.brithDate || {};
      const birthData = {
        title: '出生',
        title1: '出生时间', content1: birthInfo.brithday ? new Date(birthInfo.brithday).toLocaleDateString() : '无数据',
        title2: '公畜', content2: `IMEI：${birthInfo.father || '无数据'}`,
        title3: '母畜', content3: `IMEI：${birthInfo.mother || '无数据'}`,
        title4: '出生体重', content4: `${birthInfo.Bweigth || 0}KG`, 
        title5: '操作员', content5: birthInfo.man || '无数据'
      };
      
      // 处理发情信息
      const heatInfo = data.growthList || {};
      const heatData = {
        title: '发情', 
        title1: '预测发情时间', content1: heatInfo.F_EsTimateTime ? new Date(heatInfo.F_EsTimateTime).toLocaleDateString() : '无数据',
        title2: '实际发情时间', content2: heatInfo.F_SjTimateTime ? new Date(heatInfo.F_SjTimateTime).toLocaleDateString() : '无数据',
        title3: '配种时间', content3: heatInfo.F_Opert || '无数据'
      };
      
      // 处理配种信息
      const matingInfo = data.descentList || {};
      const matingData = {
        title: '配种',
        title1: '配种时间', content1: matingInfo.F_ReproTime ? new Date(matingInfo.F_ReproTime).toLocaleDateString() : '无数据',
        title2: '配种牲畜', content2: `IMEI：${matingInfo.F_P || '无数据'}`, 
        title3: '操作员', content3: matingInfo.F_UserName || '无数据'
      };
      
      // 处理生育信息
      const birthCattleInfo = data.birth || {};
      const birthCattleData = {
        title: '生育',
        title1: '生育时间', content1: birthCattleInfo.F_Birthday ? new Date(birthCattleInfo.F_Birthday).toLocaleDateString() : '无数据',
        title2: '生育牲畜', content2: `IMEI：${birthCattleInfo.F_IMEI || '无数据'}`, 
        title3: '操作员', content3: birthCattleInfo.F_Man || '无数据'
      };
      
      // 处理节育信息
      const contraceptionInfo = data.jyList || {};
      const contraceptionData = {
        title: '节育',
        title1: '配种时间', content1: contraceptionInfo.F_SjTimateTime ? new Date(contraceptionInfo.F_SjTimateTime).toLocaleDateString() : '无数据',
        title2: '操作员', content2: contraceptionInfo.F_Opert || '无数据'
      };
      
      // 更新步骤数据
      stepsData.value = [birthData, heatData, matingData, birthCattleData, contraceptionData];
    }
  } catch (error) {
    console.error('获取牲畜详细信息时发生错误:', error);
  }
};

onMounted(() => {
  fetchBreedingInfo();
});
</script>